package com.cenwangkeji.resident.presenter.fragment;

import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.BannerBean;
import com.cenwangkeji.resident.model.bean.CommunityNoticeBean;
import com.cenwangkeji.resident.model.bean.HealthContentBean;
import com.cenwangkeji.resident.model.bean.HealthTypeBean;
import com.cenwangkeji.resident.model.bean.ServicePackageBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.RxUtils;


import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
/**
 * @松 on 2019/10/23 13:32
 */
public class HomePresenter {
    private I mI;

    public HomePresenter(I i) {
        mI = i;

    }


    public void getBanner(){
        Observable.create(new ObservableOnSubscribe<BannerBean>() {
            @Override
            public void subscribe(ObservableEmitter<BannerBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put("mark",3);
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.banner);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, BannerBean.class ));
            }
        }).compose(RxUtils.<BannerBean>io_main())
          .subscribe(new ObserverOnNext<BannerBean>() {
              @Override
              public void onNext(BannerBean bannerbean) {
                  if (mI!=null){
                      mI.onBanner(bannerbean);
                  }

              }
          });
    }

    //获取健康分类
    public void getCategory(){
        Observable.create(new ObservableOnSubscribe<HealthTypeBean>() {
            @Override
            public void subscribe(ObservableEmitter<HealthTypeBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( "type","category");
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.health_type);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, HealthTypeBean.class ));
            }
        }).compose(RxUtils.<HealthTypeBean>io_main())
                .subscribe(new ObserverOnNext<HealthTypeBean>() {
                    @Override
                    public void onNext(HealthTypeBean bean) {
                        if (mI!=null){
                            mI.onCategory(bean);
                        }
                    }
                });

    }
    //获取健康分类列表
    public void getCategoryList(final int id){
        Observable.create(new ObservableOnSubscribe<HealthContentBean>() {
            @Override
            public void subscribe(ObservableEmitter<HealthContentBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( "typeId",id);
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.health_content);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, HealthContentBean.class ));
            }
        }).compose(RxUtils.<HealthContentBean>io_main())
                .subscribe(new ObserverOnNext<HealthContentBean>() {
                    @Override
                    public void onNext(HealthContentBean bean) {
                        if (mI!=null){
                            mI.onCategoryContent(bean);
                        }

                    }
                });

    }
    //获取健康分类列表
    public void get(){
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put(Constant.token, User.getToken());
                /*map.put("mark",3);
                map.put("userId", User.getUserId());*/
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.get( "http://wjw.hljcwkj.com/jq/zxbbj");
                MyLogger.i(json);


            }
        }).compose(RxUtils.<String>io_main())
                .subscribe(new ObserverOnNext<String>() {
                    @Override
                    public void onNext(String bean) {
                    }
                });
    }

    //社区公告
    public void getNotice(){
        Observable.create(new ObservableOnSubscribe<CommunityNoticeBean>() {
            @Override
            public void subscribe(ObservableEmitter<CommunityNoticeBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( "typeId",0);
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.community_notice);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, CommunityNoticeBean.class ));
            }
        }).compose(RxUtils.<CommunityNoticeBean>io_main())
                .subscribe(new ObserverOnNext<CommunityNoticeBean>() {
                    @Override
                    public void onNext(CommunityNoticeBean bean) {
                            if (mI != null){
                                mI.onNotice(bean);
                            }
                    }
                });
    }

    //查询健康服务包
    public void queryService(){
        Observable.create(new ObservableOnSubscribe<ServicePackageBean>() {
            @Override
            public void subscribe(ObservableEmitter<ServicePackageBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put("mark",3);
                map.put(Constant.token, User.getToken());
                map.put("userId", User.getUserId());
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.service_package_user);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, ServicePackageBean.class ));
            }
        }).compose(RxUtils.<ServicePackageBean>io_main())
                .subscribe(new ObserverOnNext<ServicePackageBean>() {
                    @Override
                    public void onNext(ServicePackageBean bean) {
                        if (mI != null){
                            mI.onServicePackage(bean);
                        }
                    }
                });

    }

    public interface I{
        void onBanner(BannerBean bannerbean);
        void onCategory(HealthTypeBean bean);
        void onCategoryContent(HealthContentBean bean);
        void onNotice(CommunityNoticeBean bean);
        void onServicePackage(ServicePackageBean bean);
    }
}
